package com.xtourms.client.sys.um;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.xtourms.client.Context;
import com.xtourms.client.Service;
import com.xtourms.client.ui.util.MessageBox;
import com.xtourms.shared.field.WorkerField;

public class MineManager extends Composite {

	private static MineManagerFrameUiBinder uiBinder = GWT
			.create(MineManagerFrameUiBinder.class);

	interface MineManagerFrameUiBinder extends
			UiBinder<Widget, MineManager> {
	}
	
	@UiField TextBox worker_name;
	@UiField PasswordTextBox worker_pwd;
	@UiField PasswordTextBox worker_pwd1;
	@UiField PasswordTextBox worker_pwd2;
	
	public MineManager() {
		initWidget(uiBinder.createAndBindUi(this));
		initMine();
	}

	private void initMine() {
		worker_name.setValue(((JSONString)Context.ins.worker.get(WorkerField.WORKER_NAME)).stringValue());
		worker_pwd.setMaxLength(20);
		worker_pwd.setValue("");
		worker_pwd1.setMaxLength(20);
		worker_pwd1.setValue("");
		worker_pwd2.setMaxLength(20);
		worker_pwd2.setValue("");
	}

	@SuppressWarnings("deprecation")
	@UiHandler("confirm")
	void confirm(ClickEvent evt){
		String oldPwd = ((JSONString)Context.ins.worker.get(WorkerField.WORKER_PWD)).stringValue();
		String inputPwd = worker_pwd.getValue();
		if(!oldPwd.equals(inputPwd)){
			MessageBox.ins.showError("原始密码不匹配!");
			worker_pwd.setValue("");
			worker_pwd1.setValue("");
			worker_pwd2.setValue("");
			return;
		}
		String pwd1 = worker_pwd1.getValue();
		String pwd2 = worker_pwd2.getValue();
		if(pwd1.isEmpty() || pwd1.length() < 6){
			MessageBox.ins.showMessage("新密码长度不够，至少为6位，最多20位!");
			worker_pwd.setValue("");
			worker_pwd1.setValue("");
			worker_pwd2.setValue("");
			return;
		}
		if(!pwd1.equals(pwd2)){
			MessageBox.ins.showMessage("新密码不匹配!");
			worker_pwd.setValue("");
			worker_pwd1.setValue("");
			worker_pwd2.setValue("");
			return;
		}
		JSONObject newWorker= (JSONObject) JSONParser.parse(Context.ins.worker.toString());
		newWorker.put(WorkerField.WORKER_NAME, new JSONString(worker_name.getValue()));
		newWorker.put(WorkerField.WORKER_PWD, new JSONString(pwd1));
		Service.umService.cOrMWorker(newWorker.toString(), new AsyncCallback<String>() {
			
			@Override
			public void onSuccess(String result) {
				Context.ins.worker = (JSONObject) JSONParser.parse(Context.ins.worker.toString());
				initMine();
				MessageBox.ins.showMessage("修改个人信息成功!");
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.ins.showError(caught.getMessage());
			}
		});
	}
	
	@UiHandler("reset")
	void reset(ClickEvent evt){
		initMine();
	}
}
